


import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_book/constant/color.dart';
import 'package:flutter_book/page/personal_page.dart';
import 'package:flutter_book/page/book_source_page.dart';
import 'package:flutter_book/util/screen_util.dart';
import 'package:get/get.dart';
import 'package:flutter_book/util/extensions/string_extensions.dart';

import 'tab_home_page.dart';


// ignore: must_be_immutable
class TabPage extends StatelessWidget {

  bool _initScreenBool = false;
  final pageController = PageController();
  List? pages;
  BottomBarController barController=BottomBarController();
  bool isSetPageControllerPage=true;
  DateTime _lastClickTime = DateTime.now();

  @override
  Widget build(BuildContext context) {
    _initScreen(context);
    return WillPopScope(
        child: AnnotatedRegion<SystemUiOverlayStyle>(
          value: SystemUiOverlayStyle.dark, //黑色是dark 白色是light
          child:Scaffold(
            appBar: null,
            body: Container(
              width: ScreenUtil.instance.width,
              height: ScreenUtil.instance.height,
              color: AppColor.white,
              padding: EdgeInsets.only(top: ScreenUtil.instance.statusBarHeight),
              child: PageView.builder(
                itemBuilder: (BuildContext context,int index) {
                  return pages![index];
                },
                itemCount: 3,
                controller: pageController,
                // 提前预加载当前pageView的下一个视图
                allowImplicitScrolling: true,
                physics: NeverScrollableScrollPhysics(), // 禁止滑动
              ),
            ),
            bottomNavigationBar: Obx(
                  (){
                if(pageController.page!=null&&isSetPageControllerPage){
                  barController.setBottomBarCurrentIndex(pageController.page!.round());
                }
                isSetPageControllerPage=true;
                return BottomNavigationBar( // 底部导航
                  items: <BottomNavigationBarItem>[
                    BottomNavigationBarItem(icon: Icon(Icons.home), label: "书库"),
                    BottomNavigationBarItem(icon: Icon(Icons.business), label: "发现"),
                    BottomNavigationBarItem(icon: Icon(Icons.school), label: "关于"),
                  ],
                  currentIndex: barController.currentIndex.value,
                  fixedColor: Colors.blue,
                  onTap: _onClickBottomBar,
                );
              },
            ),
          ),
        ),
        onWillPop: ()async{
          if (DateTime.now().difference(_lastClickTime) > Duration(seconds: 1)) {
            //两次点击间隔超过1秒则重新计时
            _lastClickTime = DateTime.now();
            "再次点击退出应用".toast;
            return false;
          }
          return true;
        }
    );
  }

  // 初始化获取屏幕数据
  void _initScreen(BuildContext context){
    if(pages==null||pages!.length<1) {
      pages = [
        TabHomePage(_setIsAutoPlayBanner),
        BookSourcePage(),
        PersonalPage(),
      ];
    }
    if (_initScreenBool == false) {
      double screenBottom = MediaQuery.of(context).padding.bottom;
      Size screenSize = MediaQuery.of(context).size;
      ScreenUtil.init(
          width: screenSize.width,
          height: screenSize.height,
          maxPhysicalSize: screenSize.width,
          bottomHeight: screenBottom);
      _initScreenBool = true;
    }
  }

  _onClickBottomBar(index){
    isSetPageControllerPage=false;
    _setAutoPlayBanner(index==0);
    pageController.jumpToPage(index);
    barController.setBottomBarCurrentIndex(index);
  }

  //是否自动播放banner的图片切换
  void _setAutoPlayBanner(bool isPlay) {
    if (_isAutoPlayBannerListener != null) {
      _isAutoPlayBannerListener!(isPlay);
    }
  }

  void Function(bool isPlay)? _isAutoPlayBannerListener;

  void _setIsAutoPlayBanner(Function(bool isPlay) call) {
    this._isAutoPlayBannerListener = call;
  }
}


class BottomBarController extends GetxController{
  var currentIndex=0.obs;

  setBottomBarCurrentIndex(int index)=>currentIndex.value=index;
}
